<template>
  <div class="integral">
    <div class="integral_header">
      <img src="@/assets/img/integral.png">
      <div class="integral_text">
        <p class="text">
          当前积分余额
        </p>
        <p class="num">
          {{ Integral.integral }}
        </p>
      </div>
    </div>
    <van-list
      v-model:loading="loading"
      :finished="finished"
      @load="loadMore"
      :immediate-check="false"
    >
      <div
        class="integeralContent"
        v-if="demandList.length > 0"
      >
        <div class="integeralContentBox">
          <div
            class="integeralContentBoxItem"
            v-for="(item,index) in demandList"
            :key="index"
          >
            <div class="flex justify_between align_center">
              <p class="color1 content3 fontBold p_w">
                {{ item.memo }}
              </p>
              <p class="color1 content3 fontBold">
                <span style="color: #FF7A1F;">{{ item.type == 0 ? '+' : '-' }}</span>{{ item.score }}积分
              </p>
            </div>
            <div class="flex justify_between align_center">
              <p class="color6 content2 font400">
                {{ item.create_time }}
              </p>
              <p class="color6 content2 font400">
                余额：{{ item.after }}
              </p>
            </div>
          </div>
        </div>
      </div>
    </van-list>
    <no-data v-if="demandList.length === 0" />
  </div>
  <div class="fexd_footer">
    <div class="footer_btn flex align_center justify_between">
      <div
        @click="back"
        class="btn_item cancle font400 content3 color4"
      >
        返回
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, defineAsyncComponent } from 'vue'
import { integralList, getUserIntegral } from '@/api'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
const NoData = defineAsyncComponent(() => import('@/components/NoData/index.vue'))
const router = useRouter()
const store = useStore()
const demandList: any = ref([])
const loading: any = ref(false)
const finished: any = ref(false)
const refreshing: any = ref(false)
const Integral: any = ref([])
const form: any = ref({
  page: 1,
  pageSize: 10
})

const getIntegral = async () => {
  Integral.value = await getUserIntegral()
}
getIntegral()

const back = () => {
  if (store.state.from.path !== '/') {
    router.back()
  } else {
    router.replace('/')
  }
}

const onRefresh = async () => {
  form.value.page = 1
  const list:any = await loadDemandList()
  demandList.value = list
  refreshing.value = false
}

const loadMore = async () => {
  form.value.page = form.value.page + 1
  const list:any = await loadDemandList()
  demandList.value = [...demandList.value, ...list]
  loading.value = false
}

const loadDemandList = async () => {
  // 异步更新数据
  const res:any =
    (await integralList({ ...form.value })) || []
  if (!Array.isArray(res) && typeof res.list === 'object') {
    const keys:any = Object.keys(res.list)
    const newRes:any = keys.map((k:any) => res.list[k])
    finished.value = newRes.length < form.value.pageSize
    return newRes
  }

  finished.value = res.list.length < form.value.pageSize
  return res
}

onRefresh()

</script>

<style lang="less" scoped>
.integral {
    padding-bottom:94px;
    .underline {
        width: 58px;
    }
    .integral_header {
        width: 100%;
        position: relative;
        img {
            width: 100%;
        }
        .integral_text {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            text-align: center;
            .num {
                font-size: 40px;
                font-weight: bold;
                color: #ff7a1f;
            }
            .text {
                font-size: 15px;
                font-weight: 400;
                color: #ed762a;
                margin-bottom: 15px;
            }
        }
    }
    .integeralContent {
        position: relative;
        top: -40px;
        width: 100%;
        height: 100%;
        background: #f7f7f7;
        border-radius: 20px 20px 0px 0px;
        .integeralContentBox {
            padding: 15px;
            .integeralContentBoxItem {
                width: 310px;
                background: #ffffff;
                border-radius: 10px;
                padding: 15px;
                margin-bottom:12px;
                div:last-child {
                    margin-top: 15px;
                }
                .p_w{
                  width: 200px;
                }
            }
        }
    }
}
.fexd_footer {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 70px;
    background-color: #fff;
    box-shadow: 0px 2px 12px 2px rgba(22, 12, 6, 0.09);

    .footer_btn {
        padding: 0 30px;
        height: 100%;
        .cancle {
            width: 100%;
            height: 44px;
            background: #85898c;
            border-radius: 5px;
            text-align: center;
            line-height: 44px;
        }
    }
}
@media (min-width: 750px) {
.integral {
    padding-bottom:94px;
    .underline {
        width: 58px;
    }
    .integral_header {
        width: 100%;
        position: relative;
        img {
            width: 100%;
        }
        .integral_text {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            text-align: center;
            .num {
                font-size: 40px;
                font-weight: bold;
                color: #ff7a1f;
            }
            .text {
                font-size: 15px;
                font-weight: 400;
                color: #ed762a;
                margin-bottom: 15px;
            }
        }
    }
    .integeralContent {
        position: relative;
        top: -40px;
        width: 100%;
        height: 100%;
        background: #f7f7f7;
        border-radius: 20px 20px 0px 0px;
        .integeralContentBox {
            padding: 15px;
            .integeralContentBoxItem {
                width: 310px;
                background: #ffffff;
                border-radius: 10px;
                padding: 15px;
                margin-bottom:12px;
                div:last-child {
                    margin-top: 15px;
                }
                .p_w{
                  width: 200px;
                }
            }
        }
    }
}
.fexd_footer {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 70px;
    background-color: #fff;
    box-shadow: 0px 2px 12px 2px rgba(22, 12, 6, 0.09);

    .footer_btn {
        padding: 0 30px;
        height: 100%;
        .cancle {
            width: 100%;
            height: 44px;
            background: #85898c;
            border-radius: 5px;
            text-align: center;
            line-height: 44px;
        }
    }
}
}
</style>
